﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using EnvDTE;
using Microsoft.VisualStudio.Shell.Interop;
using System.Diagnostics;
using System.Windows.Threading;

namespace WebAppUploader
{
    public class ProjectSyncModel : 
        INotifyPropertyChanged,
        Microsoft.VisualStudio.Shell.Interop.IVsSelectionEvents,
        IDisposable
    {

		public ProjectSyncModel()
		{
			_Current = this;
			_Dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
		}

		private static Dispatcher _Dispatcher = null;
		public static Dispatcher Dispatcher {
			get {
				return _Dispatcher;
			}
		}
		private static ProjectSyncModel _Current = null;
		public static ProjectSyncModel Current {
			get {
				return _Current;
			}
		}

        public event PropertyChangedEventHandler PropertyChanged;

        #region Property DTE
        private EnvDTE.DTE _DTE = null;
        public EnvDTE.DTE DTE
        {
            get
            {
                return _DTE;
            }
            set
            {
                _DTE = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("DTE"));
            }
        }

        #endregion

        #region Property Enabled
        private bool _Enabled = false;
        public bool Enabled
        {
            get
            {
                return _Enabled;
            }
            set
            {
                _Enabled = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Enabled"));
            }
        }

        #endregion

        #region Property ServiceProvider
        private IServiceProvider _ServiceProvider = null;
        [XmlIgnore]
        public IServiceProvider ServiceProvider
        {
            get
            {
                return _ServiceProvider;
            }
            set
            {
                _ServiceProvider = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("ServiceProvider"));
            }
        }

        #endregion
	
        private Project _WebProject;
        public Project WebProject {
            get {
                return _WebProject;
            }
        }

		#region Property ProjectModel
		private ProjectModel _ProjectModel = null;
		public ProjectModel ProjectModel
		{
			get
			{
				return _ProjectModel;
			}
			set
			{
				_ProjectModel = value;
				if (PropertyChanged != null)
					PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("ProjectModel"));
			}
		}

		#endregion
			
        public void OnUpload()
        {

        }


        uint cookie;

        internal void Load()
        {
            IVsMonitorSelection mon = (IVsMonitorSelection)ServiceProvider.GetService(typeof(IVsMonitorSelection));
            mon.AdviseSelectionEvents(this, out cookie);
            LookupSelection();
        }

        int IVsSelectionEvents.OnCmdUIContextChanged(uint dwCmdUICookie, int fActive)
        {
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        int IVsSelectionEvents.OnElementValueChanged(uint elementid, object varValueOld, object varValueNew)
        {
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        int IVsSelectionEvents.OnSelectionChanged(IVsHierarchy pHierOld, uint itemidOld, IVsMultiItemSelect pMISOld, ISelectionContainer pSCOld, IVsHierarchy pHierNew, uint itemidNew, IVsMultiItemSelect pMISNew, ISelectionContainer pSCNew)
        {
			LookupSelection();
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        private void LookupSelection()
        {
			if (DTE == null)
				return;
            if (DTE.SelectedItems.Count == 0)
            {
                _WebProject = null;
            }
            else
            {
                _WebProject = null;
                SelectedItem s = DTE.SelectedItems.Item(1);
                Project wp = s.Project;
                if (wp == null) {
                    ProjectItem i = s.ProjectItem;
                    if (i != null)
                        wp = i.ContainingProject;
                }
                if (wp!=null && wp.Kind == "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}")
                {
                    _WebProject = wp;
                }
            }

            //this.Enabled = _WebProject != null;
            LoadProject();
        }

        private void LoadProject()
        {
            Project p = this.WebProject;
			if (p == null)
			{
				this.Enabled = false;
				return;
			}
			ProjectModel = ProjectModel.Open(p.FullName, p);
			this.Enabled = ProjectModel != null;
        }



        public void Dispose()
        {
			if (DTE == null)
				return;
			DTE = null;
            IVsMonitorSelection mon = (IVsMonitorSelection)ServiceProvider.GetService(typeof(IVsMonitorSelection));
            mon.UnadviseSelectionEvents(cookie);            
        }

		#region internal static void Save()
		internal void Save()
		{
			if (ProjectModel == null)
				return;
			ProjectModel.Save();
		}
		#endregion
}
}